// pages/login/login.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    username:null,
    password:null,
    nowtext:null,

    username: '',
    password: '',
    nowtext: '',


    selectArray: [{
      "id": "999",
      "text": "学生"
    }, {
      "id": "777",
      "text": "辅导员"
    },
    {
      "id": "900",
      "text": "家长"
    },
    {
      "id": "888",
      "text": "班主任"
    },
    {
      "id": "0",
      "text": "管理人员"
    },
    {
      "id": "998",
      "text": "学生干部"
    }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDate: function (e) {
    this.setData({ nowtext: e.detail.id })
    let nowtext = e.detail.id;
    // console.log(nowtext)
    app.globalData.UserInfo.nowtext = nowtext;
  },


  loginbtnClick: function () {

    var sha1 = require('../../utils/sha1')
    var md5 = require('../../utils/md5')


    var that = this;
    var name = that.data.username

    var pwd =that.data.password
    var nowtext =that.data.nowtext
    
    if (that.data.username.length == " "|| that.data.password.length == " "){

      wx.showModal({
        title: '温馨提示',
        content: "用户名或密码不能为空"
      })
    } else if (that.data.nowtext.length == 0) {
      wx.showModal({
        title: '温馨提示',
        content: "身份未选择！"
      })

    } else {
      var mi_psw = md5.hex_md5(sha1.hex_sha1(pwd))
      // 跳转的页面 
      wx.request({
        url: 'https://nottoday.club/index.php/api/Login/doLogin',//接口地址
        data: {
          username: name,
          password: mi_psw,
          level: nowtext,
        },
        header: {
          'content-type': 'application/json'
        },
        method: "POST",
        success(res) {
                if(res.data.code === 1)
                {
                  // wx.showToast({
                  //   title: '登录成功',
                  //   icon: 'success',
                  //   duration: 2000
                  // })
                  console.log(res)
                  console.log(res.header["Set-Cookie"])
                  wx.removeStorageSync('sessionid')
                  wx.setStorageSync("sessionid", res.header["Set-Cookie"])
                  console.log(wx.getStorageSync("sessionid"))
                  wx.switchTab({
                    url: "../my/my",
                  })
                }
                else
                {
                  wx.showToast({
                    title: '登录失败',
                    icon: 'none',
                    duration: 2000
                  }) 
                }
        },error(res)
        {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
        }
      })


    }
    //发送ajax请求到服务器-登陆


  },//点击注册事件

  registeto:function(){

    wx.navigateTo({

      url: '../register/register',
    })
  },
  usernameinput: function(event) {
    this.setData({ username: event.detail.value })
    let username = event.detail.value
    app.globalData.UserInfo.username = username
  },
  passwordinput: function(event) {
    this.setData({ password: event.detail.value })
  }
})